/**
 * 
 */
package View;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import main.GamesLocator;
import main.LotusEngine;
import Game.AbstractGame;


/**
 * @author John Huffsmith
 */
@SuppressWarnings("serial")
public class GameSelectionView extends JScrollPane 
{
    LotusViewer myView;
    LotusEngine myEngine;
    private Container container;
    
    public GameSelectionView (LotusViewer view)
    {
        myView = view;
        container = new Container();
        setViewportView(container);
        createButtons();
    }
    
    private void createButtons()
    {
        container.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridy = 0;
        c.insets = new Insets(10, 10, 0, 0);
        
        
        for (String s : GamesLocator.loadGames())
        {
            s = s.substring(0, s.length() - 5);

            final AbstractGame game;

            try
            {
                game =
                    (AbstractGame) Class.forName("testgames." + s)
                                        .newInstance();
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
                continue;
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
                continue;
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
                continue;
            }

            JButton button =
                new JButton(game.getName(), new ImageIcon(game.getScreenshot()));
            button.setVerticalTextPosition(AbstractButton.TOP);
            button.setHorizontalTextPosition(AbstractButton.CENTER);
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed (ActionEvent e)
                {
                    myView.swapView(new GameMenuView(myView, game));
                    myView.loadGame(game);
                }
            });

            container.add(button, c);
            c.gridy++;
        }
    }
}
